package com.chongjing.chat.service.impl;

import com.github.pagehelper.PageHelper;
import com.chongjing.chat.domain.MessageConditionData;
import com.chongjing.chat.domain.OverviewData;
import com.chongjing.chat.mapper.StatisticsMapper;
import com.chongjing.chat.service.IStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IStatisticsServiceImpl implements IStatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;

    // 分表数量配置（放在application.yml）
    @Value("${chat.sharding.table-count:36}")
    private int shardingTableCount;

    //获取消息概览数据
    @Override
    public OverviewData getOverviewData(MessageConditionData messageConditionData) {
        // 生成分表列表
        List<String> tableList = generateTableList();

        // 设置分表列表参数
        messageConditionData.setTableList(tableList);
        PageHelper.clearPage();
        OverviewData result = statisticsMapper.countByType(messageConditionData);
        log.info("统计结果：{}", result);
        if (result != null) {
            log.info("文本消息数：{}, 音频消息数：{}, 文件消息数：{}, 图片消息数：{}, 视频消息数：{}",
                    result.getTextMessageCount(), result.getAudioMessageCount(),
                    result.getFileMessageCount(), result.getImageMessageCount(),
                    result.getVideoMessageCount());
        }
        return result;

    }

    private List<String> generateTableList() {
        List<String> tables = new ArrayList<>();
        for (int i = 0; i < shardingTableCount; i++) {
            tables.add("wfchat.t_messages_" + i);
        }
        return tables;
    }
}
